package kr.member.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.member.dao.MemberDao;
import kr.member.domain.MemberCommand;
import kr.mini.util.PagingUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;



@Controller
public class MemberListController {
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private MemberDao memberDao;
	
	@RequestMapping("/memberlist.do")
	public ModelAndView process(@RequestParam(value="pageNum",defaultValue="1")int currentPage,
								@RequestParam(value="keyField",defaultValue="")String keyField,
								@RequestParam(value="keyWord",defaultValue="")String keyWord,
								@RequestParam(value="msg",defaultValue="")String msg){
		
		
		if(log.isDebugEnabled()){
			log.debug("currentPage : " + currentPage);
			log.debug("keyField : " + keyField);
			log.debug("keyWord : " + keyWord);
		}
		System.out.println("msg : "+msg);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("keyField", keyField);
		map.put("keyWord", keyWord);
		
		System.out.println(keyField);
		System.out.println(keyWord);

		
		//총 글의 갯수 또는 검색된 글의 갯수
		int count = memberDao.getRowCount(map);
		
		PagingUtil page = new PagingUtil(currentPage, count, 15, 10, "memberlist.do");
		
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		
		List<MemberCommand> list = null;
		if(count>0){
			list = memberDao.memberList(map);
		}else{
			list = Collections.emptyList();
		}
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("memberlist");
		mav.addObject("count",count);
		mav.addObject("list",list);
		mav.addObject("msg",msg);
		mav.addObject("pagingHtml",page.getPagingHtml());
		
		return mav;
	}
	
}






